import { HoapitalAdd, UpdateHospitalSet, getHoapitalById } from '@/api/hospitalSet/HospitalSet'
import { Button, Card, Form, Input, message } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import React, { useEffect } from 'react'
import { useNavigate, useParams } from 'react-router-dom'

export default function HospitalAddOrUpdate() {
    let navigate = useNavigate()
    let [from] = useForm()
    let { id } = useParams()

    //   添加医院数据
    let finish = async () => {
        let res = from.getFieldsValue()
        if (id) {
            let value = from.getFieldsValue()
            await UpdateHospitalSet({ ...value, id })
            message.success("修改成功")
            navigate(-1)
        }
        else {
            await HoapitalAdd(res)
            navigate(-1)
            message.success("添加成功")
        }
    }
    //根据ID初始化页面
    let getById = async () => {
        let res = await getHoapitalById(id as string)
        from.setFieldsValue(res)
    }
    // 模拟生命周期
    useEffect(() => {
        getById()
    }, [])
    return (
        <Card>
            <Form
                onFinish={finish}
                form={from}
            >
                <Form.Item
                    labelCol={{ span: 2 }}
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请输入医院名称' }]}

                >
                    <Input />
                </Form.Item>
                <Form.Item
                    labelCol={{ span: 2 }}
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请输入医院编号' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    labelCol={{ span: 2 }}
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请输入Api基础路径' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    labelCol={{ span: 2 }}
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请输入联系人姓名' },
                    { pattern: /^[\u4e00-\u9fa5]{0,}$/, message: "请输入正确的联系人名字" }
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    labelCol={{ span: 2 }}
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, message: '请输入手机号' },
                    { pattern: /^1([358][0-9]|4[456789]|6[67]|7[0135678]|9[1589])\d{8}$/, message: "请输入正确的手机号" }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 2 }}>
                    <Button type="primary" htmlType="submit">
                        保存
                    </Button>
                    <Button className='ml' type="default" onClick={() => {
                        navigate(-1)
                    }}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    )
}
